package org.vesselonline.mail;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import com.google.api.services.calendar.model.Event;

public class CalendarDialogTable extends JTable {
  /**
   * For Serializable interface, value generated by Eclipse.
   */
  private static final long serialVersionUID = -2062381080898813695L;

  private CalendarDisplay calendarDisplay;

  public CalendarDialogTable(final CalendarDisplay calendarDisplay) {
    super(calendarDisplay.getTableModel());
    this.calendarDisplay = calendarDisplay;

    this.addMouseListener(new MouseAdapter() {
      public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
          new CalendarDialog(calendarDisplay.getEvents().getItems().get(rowAtPoint(e.getPoint())));
        }
      }
    });
  }

  private class CalendarDialog extends JDialog {
    /**
     * For Serializable interface, value generated by Eclipse.
     */
    private static final long serialVersionUID = -6469820007902772579L;

    private static final String DIALOG_TITLE = "Google Calendar";

    public CalendarDialog(final Event event) {
      super();

      setTitle(DIALOG_TITLE);
      setDefaultCloseOperation(DISPOSE_ON_CLOSE);

      // Summary panel
      final JPanel summaryPanel = new JPanel();
      summaryPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

      final JLabel eventSummaryLabel = new JLabel("Summary:");
      final JTextField eventSummaryTextField = new JTextField(event.getSummary(), 32);

      summaryPanel.add(eventSummaryLabel);
      summaryPanel.add(eventSummaryTextField);

      // Date & Time panel
      final JPanel dateTimePanel = new JPanel();
      dateTimePanel.setLayout(new FlowLayout(FlowLayout.LEFT));

      final JLabel eventDateTimeLabel = new JLabel("Date & Time:");
      final JLabel eventDateTimeTextField = new JLabel(DailyBriefingUtils.INSTANCE.eventDateTimeRangeToString(event));

      dateTimePanel.add(eventDateTimeLabel);
      dateTimePanel.add(eventDateTimeTextField);

      // Location panel
      final JPanel locationPanel = new JPanel();
      locationPanel.setLayout(new FlowLayout(FlowLayout.LEFT));

      final JLabel eventLocationLabel = new JLabel("Location:");
      final JTextField eventLocationTextField = new JTextField(event.getLocation(), 32);

      locationPanel.add(eventLocationLabel);
      locationPanel.add(eventLocationTextField);

      // Info panel comprising the Summary, Date & Time, and Location
      final JPanel infoPanel = new JPanel();
      infoPanel.setLayout(new BoxLayout(infoPanel, BoxLayout.Y_AXIS));

      infoPanel.add(summaryPanel);
      infoPanel.add(dateTimePanel);
      infoPanel.add(locationPanel);

      // Description textArea
      final JTextArea eventDescriptionTextArea = new JTextArea(event.getDescription(), 8, 48);
      eventDescriptionTextArea.setLineWrap(true);

      // Button panel
      final JPanel buttonPanel = new JPanel();

      final JButton saveButton = new JButton("Save");
      saveButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          setVisible(false);

          try {
            event.setSummary(eventSummaryTextField.getText());
            event.setLocation(eventLocationTextField.getText());
            event.setDescription(eventDescriptionTextArea.getText());

            DailyBriefingUtils.INSTANCE.getGoogleCalendarService().events().update(calendarDisplay.getCalendarListEntryID(), event.getId(), event).execute();

            calendarDisplay.refresh();

          } catch (IOException ioe) {
            handleException(ioe, event);
          } catch (DailyBriefingException dbe) {
            handleException(dbe, event);
          }

          dispose();
        }
      });

      final JButton cancelButton = new JButton("Cancel");
      cancelButton.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
          dispose();
        }
      });

      buttonPanel.add(saveButton);
      buttonPanel.add(cancelButton);

      add(infoPanel, BorderLayout.NORTH);
      add(eventDescriptionTextArea, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.SOUTH);

      pack();
      setLocationRelativeTo(calendarDisplay);
      setVisible(true);
    }

    private void handleException(Exception e, Event event) {
      JOptionPane.showMessageDialog(this, "An error occurred while trying to update your \"" + event.getSummary() +
          "\" event.", DIALOG_TITLE, JOptionPane.ERROR_MESSAGE);
    }
  }
}
